package com.punai.gk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.utils.SecurityUtils;
import com.punai.gk.domain.CodeLayout;
import com.punai.gk.domain.constants.GkConstants;
import com.punai.gk.mapper.CodeLayoutMapper;
import com.punai.gk.service.ICodeLayoutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 编码格式Service业务层处理
 *
 * @author zhx
 * @date 2023-06-08
 */
@Service
public class CodeLayoutServiceImpl extends ServiceImpl<CodeLayoutMapper,CodeLayout> implements ICodeLayoutService
{
    @Autowired
    private CodeLayoutMapper codeLayoutMapper;

    @Override
    public CodeLayout selectCodeLayoutByType(String type, String specId) {
        Long companyId = SecurityUtils.getCompanyId();
        CodeLayout codeLayout;
        codeLayout = codeLayoutMapper.selectCodeLayoutByType(companyId, type, specId);
        if(Objects.isNull(codeLayout)){
            codeLayout = new CodeLayout();
            codeLayout.setCompanyId(companyId);
            codeLayout.setType(type);
            codeLayout.setSpecId(specId);
            if(StrUtil.containsAny(type, GkConstants.CL_TYPE,GkConstants.JX_TYPE)){
                codeLayout.setPrefix(type);
                codeLayout.setSuffixLen(4);
            }else if(StrUtil.containsAny(type, GkConstants.GX_TYPE,GkConstants.FB_TYPE)){
                codeLayout.setPrefix(specId);
                codeLayout.setSuffixLen(3);
            }
            codeLayoutMapper.insert(codeLayout);
        }
        return codeLayout;
    }

    @Override
    public int updateCodeLayout(CodeLayout codeLayout)
    {
        CodeLayout codeLayoutEdit = new CodeLayout();
        codeLayoutEdit.setId(codeLayout.getId());
        codeLayoutEdit.setPrefix(codeLayout.getPrefix());
        codeLayoutEdit.setSuffixLen(codeLayout.getSuffixLen());
        return codeLayoutMapper.updateById(codeLayoutEdit);
    }
}
